package zhoufu.sz.sanxiao_check.ui.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import java.util.ArrayList;
import java.util.List;
import org.byteam.superadapter.recycler.OnItemClickListener;
import org.byteam.superadapter.recycler.SuperAdapter;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.ui.contract.BaseListView;
import zhoufu.sz.sanxiao_check.utils.UIUtils;
import zhoufu.sz.sanxiao_check.weiget.MultipleStatusView;

/**
 * Project : SanXiao_Checked_V2_Fix
 * User : RCP
 * Date : 2016/7/4 15:49
 * TODO : todo
 */

public abstract class BaseListFragment<T> extends BaseFragment
        implements BaseListView, BGARefreshLayout.BGARefreshLayoutDelegate,
        OnItemClickListener
{

    public BGARefreshLayout   mRefreshLayout;
    public RecyclerView       mRecyclerView;
    public MultipleStatusView mEmptyView;
    public SuperAdapter<T>    mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState)
    {
        ViewDataBinding inflate = DataBindingUtil
                .inflate(inflater, R.layout.layout_empty_recyclerview, container, false,
                        getComponent(LoginComponet.class));
        initView(inflate.getRoot());
        return inflate.getRoot();
    }


    protected abstract void initAdapter();

    private void initView(View view)
    {
        initAdapter();
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        //DividerItemDecoration itemDecoration=new DividerItemDecoration(mAct, DividerItemDecoration.VERTICAL_LIST);
        //itemDecoration.setSize(20);
        //mRecyclerView.addItemDecoration(itemDecoration);
        if ( mAdapter != null )
        {
            mRecyclerView.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
        }
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(),
                LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mRefreshLayout = (BGARefreshLayout) view.findViewById(R.id.refreshLayout);
        BGANormalRefreshViewHolder viewHolder = new BGANormalRefreshViewHolder(getActivity(), true);
        mRefreshLayout.setRefreshViewHolder(viewHolder);
        mRefreshLayout.setPullDownRefreshEnable(true);
        mRefreshLayout.setDelegate(this);

        mEmptyView = (MultipleStatusView) view.findViewById(R.id.main_multiplestatusview);
        manager.setRecycleChildrenOnDetach(true);
    }

    @Override
    public void vStart()
    {
        if ( mAdapter != null && mAdapter.getList() != null && mAdapter.getList().size() == 0 )
        {
            mEmptyView.showLoading();
        }
    }

    @Override
    public void setAdapterData(List list, int pageIndex)
    {
        if ( list.size() > 0 )
        {
            mEmptyView.showContent();
        }
        if ( pageIndex == 1 )
        {
            mAdapter.replaceAll(list);
        } else
        {
            mAdapter.addAll(list);
        }
    }

    @Override
    public void showEmptyView()
    {
        mEmptyView.showEmpty();
    }

    @Override
    public void showMessage(String str)
    {
        UIUtils.showSnackbar(getView(), str);
    }

    @Override
    public void completedLoadMore()
    {
        mRefreshLayout.endLoadingMore();
        mRefreshLayout.endRefreshing();
    }

    @Override
    public void pullFaild()
    {
        if ( mAdapter != null && mAdapter.getList().size() > 0 )
        {
            mEmptyView.showContent();
        } else
        {
            mEmptyView.showError();
        }
        mRefreshLayout.endLoadingMore();
        mRefreshLayout.endRefreshing();
    }

    @Override
    public void pullEnd()
    {
        mEmptyView.showContent();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout)
    {

    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout)
    {
        return false;
    }

    @Override
    public void onItemClick(View itemView, int viewType, int position)
    {

    }

    @Override protected void onSaveState(Bundle outState)
    {
        super.onSaveState(outState);
        outState.putInt("state", mEmptyView.getViewStatus());
        outState.putParcelableArrayList("list",
                (ArrayList<? extends Parcelable>) mAdapter.getList());
    }

    @Override protected void onRestoreState(Bundle savedInstanceState)
    {
        super.onRestoreState(savedInstanceState);
        mAdapter.replaceAll((List<T>) savedInstanceState.getParcelableArrayList("list"));
        if ( mAdapter.getList().size() == 0 )
        {
            mEmptyView.showViewByState(
                    savedInstanceState.getInt("state", MultipleStatusView.STATUS_EMPTY));
        }
    }
}
